namespace MoreLinq;

using System;
using System.Collections.Generic;
using System.Linq;

static partial class MoreEnumerable
{
    /// <summary>
    /// Bypasses a specified number of elements at the end of the sequence.
    /// </summary>
    /// <typeparam name="T">Type of the source sequence</typeparam>
    /// <param name="source">The source sequence.</param>
    /// <param name="count">The number of elements to bypass at the end of the source sequence.</param>
    /// <returns>
    /// An <see cref="IEnumerable{T}"/> containing the source sequence elements except for the bypassed ones at the end.
    /// </returns>

#if NETSTANDARD2_1 || NETCOREAPP2_0_OR_GREATER
    public static IEnumerable<T> SkipLast<T>(IEnumerable<T> source, int count)
#else
    public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source, int count)
#endif
    {
            if (source == null) throw new ArgumentNullException(nameof(source));

            return count < 1 ? source
                : source.CountDown(count, (e, cd) => (Element: e, Countdown: cd))
                    .TakeWhile(e => e.Countdown == null)
                    .Select(e => e.Element);
        }
}
